package com.bqy.myokgo;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.provider.Settings;
import android.view.View;
import android.widget.TextView;

import com.bqy.myokgo.base.MyBaseActivity;
import com.bqy.myokgo.utils.NoDoubleClickListener;
import com.bqy.myokgo.utils.ToastUtils;

import java.io.DataOutputStream;
import java.io.IOException;

public class SetUpActivity extends MyBaseActivity {

    TextView set_on, set_off, set_id;


    @Override
    public View setContentView() {
        return View.inflate(this, R.layout.activity_set_up, null);
    }

    @Override
    public void iniView() {
        super.iniView();
        set_on = findViewById(R.id.set_on);
        set_off = findViewById(R.id.set_off);
        set_id = findViewById(R.id.set_id);
        try {
            Process process = Runtime.getRuntime().exec("su");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //当前应用的代码执行目录

    @Override
    public void iniClick() {
        super.iniClick();
        set_on.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (upgradeRootPermission(getPackageCodePath())) {
                    setAirPlaneMode(SetUpActivity.this, true);
                } else {
                    ToastUtils.show("未获取ROOT");
                }
            }
        });
        set_off.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (upgradeRootPermission(getPackageCodePath())) {
                    setAirPlaneMode(SetUpActivity.this, false);
                } else {
                    ToastUtils.show("未获取ROOT");
                }
            }
        });
        set_id.setText(getID());

    }

    private String getID() {
        return Settings.System.getString(this.getContentResolver(), Settings.System.ANDROID_ID);
    }

    private void setAirPlaneMode(Context context, boolean enable) {
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN) {
            Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, enable ? 1 : 0);
        } else {
            Settings.Global.putInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, enable ? 1 : 0);
        }
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        intent.putExtra("state", enable);
        context.sendBroadcast(intent);
    }


    /**
     * 应用程序运行命令获取 Root权限，设备必须已破解(获得ROOT权限)
     *
     * @return 应用程序是/否获取Root权限
     */
    public static boolean upgradeRootPermission(String pkgCodePath) {
        Process process = null;
        DataOutputStream os = null;
        try {
            String cmd = "chmod 777 " + pkgCodePath;
            process = Runtime.getRuntime().exec("su"); //切换到root帐号
            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes(cmd + "\n");
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
        } catch (Exception e) {
            return false;
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                process.destroy();
            } catch (Exception e) {
            }
        }
        return true;

    }


}
